package com.servlet;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;

import org.apache.log4j.Logger;

import com.util.CommonTools;

import java.util.Locale;
/**
 * @author ʩ��
 * @since 2006-1-10
 * @version 1.0
 * @see
 */
public class AuthFilter implements Filter{
	Logger logger = Logger.getLogger(this.getClass());
    private FilterConfig config;
    String loginFormURI;
    String loginActionURI;

    public AuthFilter(){
  
    }

    public void init(FilterConfig config) throws ServletException{
        loginFormURI = config.getInitParameter("loginFormURI");
        if(CommonTools.isNullString(loginFormURI))
            loginFormURI = "/login.jsp";
        loginActionURI = config.getInitParameter("loginActionURI");
        if(CommonTools.isNullString(loginActionURI))
            loginActionURI = "/login.do";
        this.config = config;
        
    }

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws IOException, ServletException
    {
    	Locale loc=req.getLocale();
    	res.setLocale(loc);
        HttpServletRequest hreq = (HttpServletRequest)req;
        String path = hreq.getServletPath(); 
        logger.info("from:" + hreq.getHeader("referer"));
        logger.info("======================to:" + path);
        HttpSession session = hreq.getSession();
        Object obj = session.getAttribute("userContainer");
        if(((!path.equals("/locale.do") && obj == null && !loginFormURI.equalsIgnoreCase(path) && !loginActionURI.equalsIgnoreCase(path))))
        {   
        	return;
        }
       
        chain.doFilter(req, res);
    }

	public void destroy() {
	
	}
}
